package com.ews.onlineexchange.model;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlTransient;

import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.search.annotations.Analyzer;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;

/**
 * Entity implementation class for Entity: DataEntity
 *
 */
@MappedSuperclass
public class DataEntity implements Serializable, FullTextResult {

	
	private static final long serialVersionUID = 1L;

	protected String id;
	protected int version;

	// 删除标记（0：正常；1：删除；2：审核；）
	public static final String FIELD_DEL_FLAG = "delFlag";
	public static final String DEL_FLAG_NORMAL = "0";
	public static final String DEL_FLAG_DELETE = "1";
	public static final String DEL_FLAG_AUDIT = "2";

	protected String 				remarks; // 备注
	protected int 					sort; // 排序
	protected String 				createBy; // 创建者
	protected Date 					createDate;// 创建日期
	protected String 				updateBy; // 更新者
	protected Date 					updateDate;// 更新日期
	protected String 				delFlag; // 删除标记（0：正常；1：删除；2：审核）
	
	protected Date 					lastUpdateDate;			// 上次更新日期 目前仅当中介机构修改基本信息时，更新此值，其他实体也可使用

	protected String				highLightTitle;
	protected String				highLightContent;
	protected String 				className ;
	protected int 					rowIndex;
	public DataEntity() {
		super();
		this.delFlag = DEL_FLAG_NORMAL;
		this.sort = 30;
	}

	@PrePersist
	public void prePersist() {
		this.updateDate = new Date();
		this.createDate = this.updateDate;
	}

	@PreUpdate
	public void preUpdate() {
		this.updateDate = new Date();
	}
	@Id 
	@GenericGenerator(name="system-uuid", strategy = "uuid")
	@GeneratedValue(generator = "system-uuid")
	@DocumentId
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	@XmlTransient
	@Version
	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}
	
	public void setSort(int sort) {
		this.sort = sort;
	}
	
	@XmlTransient
	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}
	
	@XmlTransient
	@NotNull
	public Integer getSort() {
		return sort;
	}

	public void setSort(Integer sort) {
		this.sort = sort;
	}

	@XmlTransient
	public String getCreateBy() {
		return createBy;
	}

	public void setCreateBy(String createBy) {
		this.createBy = createBy;
	}

	@Temporal(TemporalType.TIMESTAMP)
	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}
	
	@XmlTransient
	public String getUpdateBy() {
		return updateBy;
	}

	public void setUpdateBy(String updateBy) {
		this.updateBy = updateBy;
	}
	
	
	public Date getLastUpdateDate() {
		return lastUpdateDate;
	}

	public void setLastUpdateDate(Date lastUpdateDate) {
		this.lastUpdateDate = lastUpdateDate;
	}

	@Temporal(TemporalType.TIMESTAMP)
	public Date getUpdateDate() {
		return updateDate;
	}

	public void setUpdateDate(Date updateDate) {
		this.updateDate = updateDate;
	}
	
	
	@Transient
	public int getRowIndex() {
		return rowIndex;
	}

	public void setRowIndex(int rowIndex) {
		this.rowIndex = rowIndex;
	}

	@XmlTransient
	@Field(analyzer = @Analyzer(impl = SmartChineseAnalyzer.class))	
	public String getDelFlag() {
		return delFlag;
	}

	public void setDelFlag(String delFlag) {
		this.delFlag = delFlag;
	}

	@XmlTransient
	@Override
	@Transient
	public String getHighLightTitle() {		
		return this.highLightTitle;
	}

	@Override
	public void setHighLightTitle(String title) {
		this.highLightTitle = title;		
	}

	@XmlTransient
	@Override
	@Transient
	public String getHighLightContent() {		
		return this.highLightContent;
	}

	@Override
	public void setHighLightContent(String content) {
		if (content == null)
			return;
		else
			this.highLightContent = content;
	}
	
	@XmlTransient
	@Transient
	public String getClassName() {
		className = this.getClass().getName();
		className = className.substring(className.lastIndexOf(".")+1,className.length());
		return className;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((className == null) ? 0 : className.hashCode());
		result = prime * result + ((createBy == null) ? 0 : createBy.hashCode());
		result = prime * result + ((createDate == null) ? 0 : createDate.hashCode());
		result = prime * result + ((delFlag == null) ? 0 : delFlag.hashCode());
		result = prime * result + ((highLightContent == null) ? 0 : highLightContent.hashCode());
		result = prime * result + ((highLightTitle == null) ? 0 : highLightTitle.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((remarks == null) ? 0 : remarks.hashCode());
		result = prime * result + sort;
		result = prime * result + ((updateBy == null) ? 0 : updateBy.hashCode());
		result = prime * result + ((updateDate == null) ? 0 : updateDate.hashCode());
		result = prime * result + version;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		DataEntity other = (DataEntity) obj;
		if (className == null) {
			if (other.className != null)
				return false;
		} else if (!className.equals(other.className))
			return false;
		if (createBy == null) {
			if (other.createBy != null)
				return false;
		} else if (!createBy.equals(other.createBy))
			return false;
		if (createDate == null) {
			if (other.createDate != null)
				return false;
		} else if (!createDate.equals(other.createDate))
			return false;
		if (delFlag == null) {
			if (other.delFlag != null)
				return false;
		} else if (!delFlag.equals(other.delFlag))
			return false;
		if (highLightContent == null) {
			if (other.highLightContent != null)
				return false;
		} else if (!highLightContent.equals(other.highLightContent))
			return false;
		if (highLightTitle == null) {
			if (other.highLightTitle != null)
				return false;
		} else if (!highLightTitle.equals(other.highLightTitle))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (remarks == null) {
			if (other.remarks != null)
				return false;
		} else if (!remarks.equals(other.remarks))
			return false;
		if (sort != other.sort)
			return false;
		if (updateBy == null) {
			if (other.updateBy != null)
				return false;
		} else if (!updateBy.equals(other.updateBy))
			return false;
		if (updateDate == null) {
			if (other.updateDate != null)
				return false;
		} else if (!updateDate.equals(other.updateDate))
			return false;
		if (version != other.version)
			return false;
		return true;
	}
}
